package cn.skyquant.quant4j.jforex.indicator;

import com.dukascopy.api.IBar;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.indicators.*;

import java.io.PrintStream;

public class Volumn1 implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;

    private IBar[] inputs;
    private int timePeriod = 4;
    private double[][] outputs = new double[1][];

    private PrintStream out;
    public void onStart(IIndicatorContext context) {
        out = context.getConsole().getOut();
        indicatorInfo = new IndicatorInfo("Volumn1", "Sums previous values", "My indicators",
                true, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {
                new InputParameterInfo("Bar", InputParameterInfo.Type.BAR),
        };
        inputParameterInfos[0].setOfferSide(OfferSide.ASK);
        optInputParameterInfos = new OptInputParameterInfo[] {
                new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                        new IntegerRangeDescription(2, 2, 100, 1))
        };
        outputParameterInfos = new OutputParameterInfo[] {
                new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,OutputParameterInfo.DrawingStyle.LINE)
        };
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex = getLookback();
        }
        if (startIndex > endIndex) {
            return new IndicatorResult(0, 0);
        }
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            double value = 0;
            for (int k = timePeriod; k > 0; k--) {
                if(inputs[i - k].getHigh()>value){
                    value = inputs[i - k].getHigh();
                }
            }
            outputs[0][j] = value;
        }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index < inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index < optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index < outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        IBar[] a = (IBar[]) array;
        for(int i=0;i<a.length;i++){
            out.println("setInputParameter["+i+"]="+a[i]);
        }
        inputs = a;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        double[] a = (double[]) array;
        for(int i=0;i<a.length;i++){
            out.println("setOutputParameter["+i+"]="+a[i]);
        }
        outputs[index] = (double[]) array;
    }
}
